package com.company.manage.system.web.controller;

import com.company.manage.common.base.BaseController;
import com.company.manage.common.persistence.Page;
import com.company.manage.common.persistence.PropertyFilter;
import com.company.manage.system.entity.User;
import com.company.manage.system.service.UserRoleService;
import com.company.manage.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by HuangYong on 2016/1/23.
 * desc:用户controller
 */
@Controller
@RequestMapping("system/user")
public class UserController extends BaseController {
    private static final String TAG = "UserController";

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list() {
        return "system/userList";
    }

    /**
     * 获取用户 JSON
     *
     * @param request
     * @return
     */
    @RequiresPermissions("sys:user:view")
    @RequestMapping(value = "json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getData(HttpServletRequest request) {
        Page<User> page = getPage(request);

        List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
        page = userService.search(page, filters);
        return getEasyUIData(page);
    }

    /**
     * 添加用户跳转
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    @RequiresPermissions("sys:user:create")
    public String createForm(Model model) {
        model.addAttribute("user", new User());
        model.addAttribute("action", "create");
        return "system/userForm";
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @RequiresPermissions("sys:user:add")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@Valid User user) {
        userService.save(user);
        return "success";
    }

    /**
     * 修改用户跳转
     *
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("sys:user:update")
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("user", userService.get(id));
        model.addAttribute("action", "update");
        return "system/userForm";
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @RequiresPermissions("sys:user:update")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@Valid @ModelAttribute @RequestBody User user) {
        userService.update(user);
        return "success";
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @RequiresPermissions("sys:user:delete")
    @RequestMapping(value = "delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id") Integer id) {
        userService.delete(id);
        return "success";
    }

    /**
     * 弹窗，用户拥有的角色
     *
     * @param userId
     * @param model
     * @return
     */
    @RequiresPermissions("sys:user:roleView")
    @RequestMapping(value = "{userId}/userRole")
    public String getUserRole(@PathVariable("userId") Integer userId, Model model) {
        model.addAttribute("userId", userId);
        return "system/userRoleList";
    }

    /**
     * 获取用户拥有的角色id集合
     *
     * @param id
     * @return
     */
    @RequestMapping("{id}/role")
    @RequiresPermissions("sys:user:roleView")
    @ResponseBody
    public List<Integer> getRoleIdList(@PathVariable("id") Integer id) {
        return userRoleService.getRoleIdList(id);
    }

    /**
     * 修改用户拥有的角色
     *
     * @param id
     * @param newRoleIds
     * @return
     */
    @RequiresPermissions("sys:user:roleUpd")
    @RequestMapping(value = "{id}/updateRole")
    @ResponseBody
    public String updateUserRole(@PathVariable("id") Integer id, @RequestBody List<Integer> newRoleIds) {
        userRoleService.updateUserRole(id, userRoleService.getRoleIdList(id), newRoleIds);
        return "success";
    }

    /**
     * 修改密码跳转
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "updatePwd", method = RequestMethod.GET)
    public String updatePwdForm(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);
        return "system/updatePwd";

    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param user
     * @param session
     * @return
     */
    @RequestMapping(value = "pudatePwd", method = RequestMethod.POST)
    @ResponseBody
    public String updatePwd(String oldPassword, @Valid @ModelAttribute @RequestBody User user, HttpSession session) {
        if (userService.checkPassword((User) session.getAttribute("user"), oldPassword)) {
            userService.updatePwd(user);
            session.setAttribute("user", user);
            return "success";
        } else {
            return "fail";
        }
    }

    /**
     * Ajax请求校验loginName是否唯一。
     *
     * @param loginName
     * @return
     */
    @RequestMapping(value = "checkLoginName")
    @ResponseBody
    public String chechLoginName(String loginName) {
        if (userService.getUser(loginName) == null) {
            return "true";
        } else {
            return "false";
        }
    }
    @RequiresPermissions("sys:user:update")
    @RequestMapping(value = "checkPwd")
    @ResponseBody
    public String checkPwd(String oldPassword, HttpSession session) {
        if (userService.checkPassword((User) session.getAttribute("user"), oldPassword)) {
            return "true";
        } else {
            return "false";
        }
    }

    /**
     * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2
     * Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
     * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
     */
    @ModelAttribute
    public void getUser(@RequestParam(value = "id",defaultValue = "-1") Integer id,Model model){
        if(id!=-1){
            model.addAttribute("user",userService.get(id));
        }
    }
} 